package com.umeox.watch.moto.dataservice.services;

import android.app.IntentService;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class CopyFileService extends IntentService {

    private final static String TAG = CopyFileService.class.getSimpleName();

    private static String PRELOAD_SRC = "/system/media/audio/ringtones/";//*** 这个路径是/system/loadmedia
    private static String PRELOAD_DEST = "/storage/emulated/0/Music/";

    public CopyFileService() {
        super(TAG);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        if (copyFolder(new File(PRELOAD_SRC), new File(PRELOAD_DEST))) {
            Log.e(TAG, "doPreloadMedia success !");
        } else {
            Log.e(TAG, "doPreloadMedia failed 。");
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e(TAG, "========onCreate=======");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e(TAG, "========onDestroy=======");
    }

    //added by yuegang start
    public static boolean copyFolder(File srcFile, File destFile) {
        if (!srcFile.isDirectory()) {
            return false;
        }
        if (!destFile.exists() && !destFile.mkdirs()) {
            return false;
        }


        Log.d("yue", "copy1");
        boolean result = true;
        File[] list = srcFile.listFiles();
        if (list == null) {
            return result;
        }
        for (File f : list) {
            Log.d("yue", "123");
            if (f.isDirectory()) {
                result &= copyFolder(f, new File(destFile, f.getName()));
            } else {
                if (getFileExtension(f).equals("mp3")) {

                    Log.d("yux", "f=" + f.getName());
                    String fileName = f.getName();
                    String newName = fileName.replace("_", " ");
                    //android.util.Log.d("yux",""+f.getParent()+"");
                    Log.d("yux", "newName=" + newName);
                    //File f_new = new File(f.getParent(),newName);
                    //android.util.Log.d("yux","f_new="+f_new.toString());
                    //boolean isSuccess =f.renameTo(f_new);
                    //android.util.Log.d("yux",isSuccess+" ==");
                    // result &= copyFile(f,new File(destFile,f.getName()));
                    result &= copyFile(f, new File(destFile, newName));
                }
            }
        }
        return result;
    }


    //added by yuegang end


    /*
        public boolean copyFolder(File srcFile, File destFile) {
            if (!srcFile.isDirectory()) {
                Log.i(TAG, "srcFile no isDirectory :"+srcFile.getName());
                return false;
            }

            if(!(getFileExtension(srcFile).equals("mp3"))){
                return false;
            }

            if (!srcFile.canRead()) {
                Log.i(TAG, "no canRead :"+srcFile.getName());

            }
            if (!srcFile.canWrite()) {
                Log.i(TAG, "no canWrite :"+srcFile.getName());
            }

            if (!destFile.exists()) {
                boolean r = false;
                synchronized (this) {
                    r = destFile.mkdirs();
                }
                Log.i(TAG, "destFile:exists " + r);
            }
            boolean result = true;
            File[] list = srcFile.listFiles();

            if (list == null) {
                Log.i(TAG, "copyFolder: list null =" + srcFile.getName());
                return false;
            }
            for (File f : list) {
                if (f.isDirectory()) {
                    result &= copyFolder(f, new File(destFile, f.getName()));
                } else if(f.isFile()){
                    result &= copyFile(f, new File(destFile, f.getName()));
                }
            }
            return result;
        }
    */
    private static String getFileExtension(File file) {
        String fileName = file.getName();
        if (fileName.lastIndexOf(".") != -1 && fileName.lastIndexOf(".") != 0)
            return fileName.substring(fileName.lastIndexOf(".") + 1);
        else return "";
    }


    private static boolean copyFile(File srcFile, File destFile) {
        boolean result = false;
        try {
            InputStream in = new FileInputStream(srcFile);
            try {
                Log.d("yue", "1111");
                result = copyToFile(in, destFile);
            } finally {
                in.close();
            }
        } catch (IOException e) {
            result = false;
        }
        return result;
    }


    /**
     * Copy data from a source stream to destFile.
     * Return true if succeed, return false if failed.
     */


    private static boolean copyToFile(InputStream inputStream, File destFile) {
        try {
            if (destFile.exists()) {
                destFile.delete();
            }
            FileOutputStream out = new FileOutputStream(destFile);
            try {
                //byte[] buffer = new byte[4096];
                byte[] buffer = new byte[1024];
                int bytesRead;
                Log.d("yue", "bytesRead");
                while ((bytesRead = inputStream.read(buffer)) >= 0) {
                    out.write(buffer, 0, bytesRead);
                }
            } finally {
                out.flush();
                try {
                    out.getFD().sync();
                } catch (IOException e) {
                }
                out.close();
            }
            return true;
        } catch (IOException e) {
            return false;
        }
    }


}



